﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web.Mvc;

using Sherwood.SignOn.Server;
using Sherwood.SignOn.Server.Models;
using Sherwood.SignOn.Server.Models.ViewModels;
using Sherwood.SignOn.Server.Controllers;
using Sherwood.SignOn.Server.Models.Repositories;
using Sherwood.SignOn.Server.Models.Services;


namespace Sherwood.SignOn.Server.Tests
{
    public class TestHelper
    {
        public static bool HasError(ModelStateDictionary state)
        {
            foreach (string key in state.Keys)
            {
                if (state[key].Errors.Count > 0)
                    return true;
            }
            return false;
        }

        public static bool HasError(string field, ModelStateDictionary state)
        {
            if (state.ContainsKey(field))
            {
                return state[field].Errors.Count > 0;
            }
            return false;
        }

        public static IUserAccount RegisterValidDummyUser(string username, string email)
        {
            return RegisterValidDummyUser(username, email, "Test", "User");
        }

        public static IUserAccount RegisterValidDummyUser(string username, string email, string firstName, string lastName)
        {

                RegisterViewModel registerModel = new RegisterViewModel();
                registerModel.UserName = username;
                registerModel.Email = email;
                registerModel.Password = "password";
                registerModel.Password2 = "password";
                registerModel.FirstName = firstName;
                registerModel.LastName = lastName;
                registerModel.RequestedUrl = "";
                registerModel.Gender = Gender.Male;
                registerModel.DateOfBirth = new DateTime(2010, 1, 1);
                registerModel.CultureName1 = "en-US";
                registerModel.CultureName2 = "en-US";
                registerModel.CultureName3 = "en-US";
                registerModel.Country = "US";

                UserAccountController registerController = new UserAccountController();
                registerController.Register(registerModel); //Create user

                return Data.UserAccounts.GetUserAccountByUsername(username);

        }


        public static IUserSession CreateDummySession()
        {
            IUserAccount user = TestHelper.CreateVerifiedDummyUser("username", "password");
            //Signin
            SignInViewModel viewModel = new SignInViewModel();
            viewModel.UserName = "username";
            viewModel.Password = "password";
            viewModel.ClientSessionId = Guid.NewGuid().ToString();
            viewModel.ReturnUrl = "http://somewhere.sherwood.codeplex.com/SignIn/?ReturnUrl=bogus&ticket={signinticket}&signature={signinsignature}";
            viewModel.SignOutUrl = "http://somewhere.sherwood.codeplex.com/SignOut/?ticket={signoutticket}&signature={signoutsignature}";
            SignInController controller = new SignInController();
            RedirectResult result = controller.Index(viewModel) as RedirectResult;

            return UserSessionHelper.GetCurrentUserSession();

        }

        public static IUserAccount CreateVerifiedDummyUser(string username, string password)
        {
            CreateUserAccountParams parameters = new CreateUserAccountParams
            {
                UserName = username,
                Password = password,
                Email = "joe.bloggs@sherwood.codeplex.com",
                LastName = "Bloggs",
                FirstName = "Joe",
                Gender = Gender.Male,
                IsEnabled = true,
                VerificationStatus = VerificationStatus.Verified,
                CultureName1 = "en-US",
                CultureName2 = "en-US",
                CultureName3 = "en-US",
                Country = "US"
            };
            return Data.UserAccounts.CreateUserAccount(parameters);

        }

        public static string GetResourceFile(string filename)
        {
            filename = Context.ResourcesDirectory + "\\" + filename;
            System.IO.FileInfo fi = new System.IO.FileInfo(filename);
            System.IO.StreamReader reader = fi.OpenText();
            string contents = reader.ReadToEnd();
            reader.Close();
            return contents;
        }

        public static string GetSignature(string message, string key)
        {

            //string privateKey = GetResourceFile(keyFilename);
            string privateKey = System.Configuration.ConfigurationManager.AppSettings[key];

            Sherwood.Security.Rsa rsa = Sherwood.Security.Rsa.FromPrivateKey(privateKey);

            return rsa.SHA1Sign(message);
        }
    }
}
